using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MyAlbum.Services;
using BlogML.Xml;
using MyAlbum.Utility;
using MyAlbum.Model;

namespace PersonalAlbum.Controllers
{
    [ValidateInput(false)]  
    public class MiniBlogController : ApplicationController
    {
        private IBlogService _blogService;
        private ILanguageService _langService;
        private IConfiguration _config;
        public MiniBlogController()
            : this(ObjectFactory.GetInstance<IBlogService>(),
                ObjectFactory.GetInstance<ILanguageService>(),
                ObjectFactory.GetInstance<IConfiguration>())
        { }

        public MiniBlogController(IBlogService blogService,
            ILanguageService langService,
            IConfiguration configService)
        {
            this._blogService = blogService;
            this._config = configService;
            this._langService = langService;
        }
        //
        // GET: /Blog/
        // GET: /Blog/2
        /*TODO output cache*/
        public ActionResult Index(int page)
        {
            ((PersonalAlbumModel)ViewData.Model).Posts = _blogService.GetPosts(page, this._config.PostsPerPage, null);
            return View(ViewData.Model);
        }

        //
        // GET: /Blog/blogId

        public ActionResult Details(string id)
        {
            try
            {
                var post = this._blogService.GetPost(id);
                if (post != null)
                {
                    var model = ((PersonalAlbumModel)ViewData.Model);
                    model.Post = post;
                    //var replies = new List<Reply>();
                    //foreach (BlogMLComment reply in post.Comments)
                    //{
                    //    replies.Add(new Reply(){
                    //         Body=reply.Content.Text,
                    //          Email=reply.UserEMail,
                              
                    //}
                    return View(model);
                }
                else
                {
                    return Index(1);
                }
            }
            catch (Exception e1)
            {
                WebLogger.LogError(e1, "get post id :" + id);
                return Index(1);
            }
        }



        //
        // GET: /Blog/Tag/tagName

        public ActionResult Tag(string id)
        {
            try
            {
                 ((PersonalAlbumModel)ViewData.Model).Posts= this._blogService.GetPosts(new Tag() { Name = id }, null, 15);
                 return View("Index", ViewData.Model);
            }
            catch (Exception e1)
            {
                WebLogger.LogError(e1, "get posts for tag :" + id);
                return Index(1);
            }
        }

        //
        // GET: /Blog/Create

        [Authorize(Roles = RolesManager.AddEntriesRoles)]
        public ActionResult Create()
        {
            return View(ViewData.Model);
        }

        //
        // POST: /Blog/Create

        [ValidateInput(false)]  
        [Authorize(Roles = RolesManager.AddEntriesRoles)]
        [AcceptVerbs(HttpVerbs.Post)]
        // [ValidateAn tiForgeryToken()]
        public ActionResult Create(PostEntry post)
        {
            try
            {
                var summary = new ModelStateWrapper(ViewData.ModelState);
                this._blogService.AddPost(post, Request.Url, summary);
                if (summary.IsValid)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(ViewData.Model);
                }
            }
            catch(Exception e1)
            {
                WebLogger.LogError(e1);
                return View(ViewData.Model);
            }
        }

        //
        // GET: /Blog/Edit/5

        [ValidateInput(false)]  
        [Authorize(Roles = RolesManager.AdminRoles)]
        public ActionResult Edit(string id)
        {
            var post = this._blogService.GetPost(id);
            if (post != null)
            {
                ((PersonalAlbumModel)ViewData.Model).Post = post;
                return View(ViewData.Model);
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Blog/Delete/5
        // Ajax request
        [Authorize(Roles = RolesManager.AdminRoles)]
        [AcceptVerbs(HttpVerbs.Post)]
        // [ValidateAn tiForgeryToken()]
        public ActionResult Delete(string id)
        {
            try
            {
                this._blogService.DeletePost(id);
                if (Request.IsAjaxRequest())
                {
                    return Json(new { success = true });
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch(Exception e1)
            {
                WebLogger.LogError(e1);
                if (Request.IsAjaxRequest())
                {
                    return Utils.JavascriptError("");
                }
                else
                {
                    return View(ViewData.Model);
                }
            }
        }

        //
        // POST: /Blog/Edit/5
        [Authorize(Roles = RolesManager.AdminRoles)]
        [AcceptVerbs(HttpVerbs.Post)]
        // [ValidateAn tiForgeryToken()]
        public ActionResult Edit(PostEntry post)
        {
            try
            {
                var summary = new ModelStateWrapper(ViewData.ModelState);
                this._blogService.UpdatePost(post, summary);
                if (summary.IsValid)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(ViewData.Model);
                }
            }
            catch(Exception e1)
            {
                WebLogger.LogError(e1);
                return View(ViewData.Model);
            }
        }
    }
}
